package org.kgc.alanhealth.controller;

import org.kgc.alanhealth.entity.HospitalAntidiabetics;
import org.kgc.alanhealth.entity.HospitalArticle;
import org.kgc.alanhealth.entity.HospitalEncyclopedia;
import org.kgc.alanhealth.service.impl.BaikeServiceImpl;
import org.kgc.alanhealth.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("Baike")
@CrossOrigin(origins = "*")
public class BaikeController {
    @Autowired
    private BaikeServiceImpl baikeService;

    @RequestMapping("baikeHot")
    public Result baikeHotList(Integer currentPageNo,Integer pageSize) {
        List<HospitalArticle> hotArticlesList = baikeService.getHotArticles(currentPageNo,pageSize);
        if (hotArticlesList != null && !hotArticlesList.isEmpty()) {
            return Result.success(hotArticlesList);
        } else
            return Result.error("not found");
    }
    @RequestMapping("querycount")
    public Result getAllArticlesCount(){
        int list=baikeService.getAllByArticlesCount();
        if (list!=0){
            return Result.success(list);
        }else{
            return Result.error("没有找到数据");
        }
    }
@RequestMapping("/menuFirstFind")
public Result menuFirstFind() {
    List<HospitalEncyclopedia> encyclopediaList = baikeService.getAllRecords();
    if (encyclopediaList != null && !encyclopediaList.isEmpty()) {
        // 创建一个新列表来保存需要的信息
        List<Map<String, Object>> resultList = encyclopediaList.stream()
                .map(item -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", item.getId());
                    map.put("encyname", item.getEncyname());
                    return map;
                })
                .collect(Collectors.toList());

        return Result.success(resultList);
    } else {
        return Result.error("not found");
    }
}
    @RequestMapping(value = "/menuSecondFindByEncyName", method = RequestMethod.GET)
    public Result menuSecondFindByEncyName(@RequestParam("encyName") String encyName) {
        // 使用 Service 层方法来获取数据
        List<HospitalAntidiabetics> antidiabeticsList = baikeService.findAntidiabeticsByEncyName(encyName);
        if (antidiabeticsList != null && !antidiabeticsList.isEmpty()) {
            return Result.success(antidiabeticsList);
        } else
            return Result.error("not found");
    }

    ////HospitalAntidiabetics的encyclopediaId与HospitalArticle的antiId查询文章的标题antilTitle
    @RequestMapping(value = "/baikeArticleFindByEncyName", method = RequestMethod.GET)
    public Result baikeArticleFindByEncyName(@RequestParam("encyName") String encyName) {
        // 使用 Service 层方法来获取数据
        List<HospitalArticle> articleList = baikeService.findArticlesByEncyName(encyName);
        if (articleList != null && !articleList.isEmpty()) {
            return Result.success(articleList);
        } else
            return Result.error("not found");
    }
    //根据HospitalArticle的antilTitle查询文章内容
    @RequestMapping("/baikeFindByAntiTitle")
    public Result baikeFindByAntiTitle(@RequestParam("antiTitle") String antiTitle){
        List<HospitalArticle> articleList = baikeService.findArticlesByAntiTitle(antiTitle);
        if (articleList != null && !articleList.isEmpty()) {
            return Result.success(articleList);
        }
        return Result.error("not found");
    }


}


